import { Component, OnInit } from '@angular/core';
import { NzModalService, NzMessageService } from 'ng-zorro-antd';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { resetFakeAsyncZone } from '@angular/core/testing';
import { ResultList } from '../result-list';
import { forEach } from '@angular/router/src/utils/collection';
import { Observable } from 'rxjs/Observable';
import { UserFormComponent } from '../user-form/user-form.component'
import { UserUploadFormComponent } from '../user-upload-form/user-upload-form.component';
import { RequestMapping } from '../request.mapping';
import { FormBuilder } from '@angular/forms/src/form_builder';


@Component({
  selector: 'app-user-manager',
  templateUrl: './user-manager.component.html',
  styleUrls: ['./user-manager.component.css']
})

export class UserManagerComponent implements OnInit {
  isCollapsed = false;
  page: any;
  excel = RequestMapping.USER_EXCEL_TEMPLATE;

  _dataSet = [];
  _allChecked = false;
  _indeterminate = false;
  constructor(public modalService: NzModalService, public http: HttpClient, public messageService: NzMessageService) { }
  _value = '';
  ngOnInit() {
    this.loadData();

  }

  loadData() {
    this.http.get<ResultList>(RequestMapping.USERS).subscribe(response => {
      this._dataSet = response.data;
    })
  }

  onSearch() {
    alert("jj")
  }

  downloadTemplate(){
    return true;
  }

  /**
   * 添加窗口
   */
  addModal() {
    const subscription = this.modalService.open({
      title: '添加用户',
      content: UserFormComponent,
      onOk() {
        //现在添加
      },
      onCancel() {
        console.log('Click cancel');
      },
      footer: true,
      componentParams: {
        name: '测试渲染Component'
      }
    });
    subscription.subscribe(result => {
      console.log(result);
    })
  }

  //删除
  delete() {
    var ids = [];
    for (let d of this._dataSet) {
      if (d.checked) {
        ids.push(d.id);
      }
    }
    console.log("要删除的有>>>" + ids);

    const httpOptions = {
      headers: new HttpHeaders({ 'Content-Type': 'application/json', ids: ids })
    };
    this.http.delete<ResultList>(RequestMapping.USERS, httpOptions).subscribe(response => {
      if (response.status == 200) {
        this.modalService.success({ title: "成功", content: "删除成功" })
        // this.messageService.create("success", "删除成功", { nzDuration: 3500 });
      }
      this.loadData();
      // remove 掉这些数据
      // for(let i in this._dataSet){
      //   if(this._dataSet[i].checked){
      //     this._dataSet.slice(parseInt(i),1);
      //   }
      // }
    })

  }

  showDeleteConfirm() {
    var comfirm = this.modalService.confirm({
      title: '您是否要删除所有所选记录',
      content: '<b>这将会删除和当前用户有关的所有数据。</b>',
      onOk() {
        this.delete();
      },
      onCancel() {
      }
    });

  };

  /**
   * 导入用户数据
   */
  openImportModal() {
    this.modalService.open({ title: "上传", content: UserUploadFormComponent,footer:false})
  }

  _displayDataChange($event) {
    this._dataSet = $event;
    this._refreshStatus();
  }

  _refreshStatus() {
    const allChecked = this._dataSet.every(value => value.checked === true);
    const allUnChecked = this._dataSet.every(value => !value.checked);
    this._allChecked = allChecked;
    this._indeterminate = (!allChecked) && (!allUnChecked);
  }

  _checkAll(value) {
    if (value) {
      this._dataSet.forEach(data => {
        data.checked = true;
      });
    } else {
      this._dataSet.forEach(data => {
        data.checked = false;
      });
    }
    this._refreshStatus();
  }

}
function userForm(): string {
  return "userUploadForm";
}

